Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail gracefully when line number is out-of-bounds and negative #10868

Closed
wants to merge 2 commits into from

Conversation

jakevdp
Copy link
Contributor

@jakevdp jakevdp commented Apr 5, 2023

This fixes an issue I ran into that is similar to #752.

I don't have a self-contained repro for this failure, but an example of the failure is in the nightly CI for the JAX project: https://github.com/google/jax/actions/runs/4607513902/jobs/8142126075. The last few lines of the traceback looks like this:

INTERNALERROR> E                 reprtraceback = self.repr_traceback(excinfo_)
INTERNALERROR> E                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR> E               File "/opt/hostedtoolcache/Python/3.11.2/x64/lib/python3.11/site-packages/_pytest/_code/code.py", line 871, in repr_traceback
INTERNALERROR> E                 reprentry = self.repr_traceback_entry(entry, einfo)
INTERNALERROR> E                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR> E               File "/opt/hostedtoolcache/Python/3.11.2/x64/lib/python3.11/site-packages/_pytest/_code/code.py", line 822, in repr_traceback_entry
INTERNALERROR> E                 s = self.get_source(source, line_index, excinfo, short=short)
INTERNALERROR> E                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR> E               File "/opt/hostedtoolcache/Python/3.11.2/x64/lib/python3.11/site-packages/_pytest/_code/code.py", line 751, in get_source
INTERNALERROR> E                 lines.append(space_prefix + source.lines[line_index].strip())
INTERNALERROR> E                                             ~~~~~~~~~~~~^^^^^^^^^^^^
INTERNALERROR> E             IndexError: list index out of range

Reading through the source, I see that positive out-of-bound indices are already handled gracefully:

if source is None or line_index >= len(source.lines):
source = Source("???")
line_index = 0

Given this, I suspect the only possible way to hit the error I'm seeing is if the list index is out of range and negative.

@nicoddemus
Copy link
Member

Seems similar/duplicate to #10840.

@jakevdp
Copy link
Contributor Author

jakevdp commented Apr 5, 2023

Yep, looks like a duplicate. Consider this a +1 for getting that PR in ASAP!

@jakevdp jakevdp closed this Apr 5, 2023
@jakevdp jakevdp deleted the fix-lineno branch April 5, 2023 00:14
@jakevdp
Copy link
Contributor Author

jakevdp commented Apr 5, 2023

It looks like that PR is stalled on lack of tests – would it help to pull in the tests I wrote here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants